package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.dto.AttrGroupDto;
import com.woniuxy.goods.dto.AttrInfoDto;
import com.woniuxy.goods.entity.AttributeInfo;
import com.woniuxy.goods.form.AddAttrInfoForm;
import com.woniuxy.goods.form.FindConfigPageForm;
import com.woniuxy.goods.form.UpdateAttrInfoForm;
import com.woniuxy.goods.param.AddAttrInfoParam;
import com.woniuxy.goods.param.FindConfigPageParam;
import com.woniuxy.goods.param.UpdateAttrInfoParam;
import com.woniuxy.goods.service.AttributeInfoService;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 * 属性控制层接口
 * @author ...
 * @since 2024年05月25日
 */
@RestController
@Api(tags = "属性管理接口")
@RequestMapping("/attributeInfo")
public class AttributeInfoController {
    /**
     * 属性服务层接口
     */
    @Resource
    private AttributeInfoService attributeInfoService;

    /**
     * 新增典当物品属性信息
     * @param form 新增属性对象
     * @return 属性信息
     * @throws Exception
     */
    @ApiOperation("新增典当物品属性信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttributeInfo.class)
    })
    @PostMapping("/addAttrInfo")
    @Permission(roles = {"超级管理员","管理员"})
    public Result addAttrInfo(@Validated @RequestBody AddAttrInfoForm form, BindingResult br)throws Exception{
        if(br.hasErrors()){
            throw new ParamException(10001,"参数有误");
        }
        if (ObjectUtil.isNotEmpty(form.getList())){
            Set<String> list = form.getList();
            for (String s : list) {
                if (ObjectUtil.isEmpty(s)){
                    list.remove(s);
                }
            }
        }
        AddAttrInfoParam param = BeanUtil.toBean(form, AddAttrInfoParam.class);
        if (ObjectUtil.isNotEmpty(form.getList())){
            param.setChoiceList(JSONUtil.toJsonStr(form.getList()));
        }
        AttributeInfo info = attributeInfoService.addAttrInfo(param);
        return Result.success("新增典当物品属性信息成功",info);
    }

    /**
     * 删除典当物品属性信息
     * @param attributeId 属性id
     * @return
     * @throws Exception
     */
    @ApiOperation("删除典当物品属性信息")
    @ApiImplicitParam("属性id")
    @GetMapping("/delAttrInfo")
    @Permission(roles = {"超级管理员","管理员"})
    public Result delAttrInfo(Integer attributeId)throws Exception{
        attributeInfoService.delAttrInfo(attributeId);
        return Result.success("删除典当物品属性信息成功",null);
    }

    /**
     * 修改典当物品属性信息
     * @param form 修改属性对象
     * @return 属性信息
     * @throws Exception
     */
    @ApiOperation("修改典当物品属性信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttributeInfo.class)
    })
    @PostMapping("/updateAttrInfo")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updateAttrInfo(@Validated @RequestBody UpdateAttrInfoForm form, BindingResult br)throws Exception{
        if(br.hasErrors()) throw new ParamException(10001,"参数有误");
        UpdateAttrInfoParam param = BeanUtil.toBean(form, UpdateAttrInfoParam.class);
        param.setChoiceList(JSONUtil.toJsonStr(form.getList()));
        AttributeInfo info = attributeInfoService.updateAttrInfo(param);
        return Result.success("修改典当物品属性信息成功",info);
    }

    /**
     * 根据id查询典当物品属性信息
     * @param attributeId 属性id
     * @return 属性信息
     * @throws Exception
     */
    @ApiOperation("根据id查询典当物品属性信息")
    @ApiImplicitParam("属性id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrInfoDto.class)
    })
    @GetMapping("/findAttrInfoById")
    public Result findAttrInfoById(Integer attributeId)throws Exception{
        AttrInfoDto info = attributeInfoService.findAttrInfoById(attributeId);
        return Result.success("根据id查询典当物品属性信息成功",info);
    }

    /**
     * 根据ids查询典当物品属性信息
     * @param attributeIds 属性id集合
     * @return 属性信息集合
     * @throws Exception
     */
    @ApiOperation("根据ids查询典当物品属性信息")
    @ApiImplicitParam("属性id集合")
    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "OK",response = AttrInfoDto.class)
    })
    @PostMapping("/findAttrInfosByIds")
    public Result findAttrInfosByIds(@RequestBody Set<Integer> attributeIds)throws Exception{
        List<AttrInfoDto> infoDtos = attributeInfoService.findAttrInfosByIds(attributeIds);
        return Result.success("根据ids查询典当物品属性信息成功",infoDtos);
    }

    /**
     * 分页查询属性
     * @param form 分页参数对象
     * @return 属性分页对象
     * @throws Exception
     */
    @ApiOperation("分页查询属性")
    @ApiImplicitParam("属性名称")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrGroupDto.class)
    })
    @PostMapping("/findAttrPage")
    @Permission(roles = {"超级管理员","管理员"})
    public Result findAttrPage(@RequestBody FindConfigPageForm form)throws Exception{
        IPage<AttrInfoDto> attrPage = attributeInfoService.findAttrPage(BeanUtil.toBean(form, FindConfigPageParam.class));
        return Result.success("分页查询属性成功",attrPage);
    }

}

